//
//  ImageLoader.swift
//  Tiktok
//
//  Created by xmd on 11/5/20.
//

import Foundation
import Combine
import UIKit

class ImageLoader : ObservableObject{
    @Published var data: UIImage?
    private var cancellable: AnyCancellable?
    init(url: String) {
        cancellable?.cancel()
        cancellable = URLSession.shared.dataTaskPublisher(for: URL(string: url)!)
            .map {data, urlResponse in UIImage(data: data)}
            .receive(on: DispatchQueue.main)
            .replaceError(with: nil)
            .assign(to: \.data, on: self)
    }
}
